<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller as BaseController;
use App\Repository\Order\MoreOrderRepository as model;
use App\Repository\UploadRepository;
use Illuminate\Http\Request;

// 分销商
class MoreOrderController extends BaseController
{
    private $repoist;

    public function __construct(model $model)
    {
        $this->repoist = $model;
    }

    public function store(Request $request)
    {
        return $this->repoist->add($request);
    }

    public function index(Request $request)
    {
        $where = [];
        if (($request->get('user')->role != 1)) {
            $where['where']['shop_id'] = $request->get('user')->shop_id;
        }
        if (!empty($request->get('phone'))) {
            $where['where']['phone'] = $request->get('phone');
        }
        if (!empty($request->get('name'))) {
            $where['like'] = ['name' => $request->get('name')];
        }
        if (!empty($request->get('number'))) {
            $where['like'] = ['name' => $request->get('number')];
        }
        $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
        $where['limit']['number'] = $request->get('pageSize');
        $data = $this->repoist->index($where);
        return $data;
    }

    public function save(Request $request)
    {
        if (!$request->get('id')) {
            return $this->returnMsg(1, '请传递id参数');
        }
        $where = ['id' => $request->get('id')];
        return $this->repoist->edit($where, $request);
    }

    public function show($id)
    {
        $where = ['id' => $id];
        //查询一条数据
        $return = $this->repoist->getOne($where, ['id', 'name', 'product_id', 'goods_point', 'goods_detail', 'goods_remark', 'main_pic', 'settle_require']);
        if (!empty($return)) {
            // $return = usfirstArr($return);
            // 将文本模式 格式化
            if (!empty($return->goods_detail)) {
                $return->goods_detail = html_entity_decode($return->goods_detail);
            }
            if (!empty($return->goods_remark)) {
                $return->goods_remark = html_entity_decode($return->goods_remark);
            }
        }
        return $this->returnMsg(200, '查询成功', $return);
    }

    public function listss(Request $request)
    {
        $where = [];
        if (!empty($request->get('name'))) {
            $where['where'] = ['name' => $request->get('name')];
        }

        $return = $this->repoist->lists($where);
        return $return;
    }

    public function destroy(Request $request, $id)
    {
        if (!$id) {
            return $this->returnMsg(1, '请传递参数');
        }
        $where = [
            'id' => $id,
            'shop_id' => $request->user->shop_id
        ];
        return $this->repoist->del($where);
    }

    // 导入功能
    public function import(Request $request)
    {
        $model = new UploadRepository();
        $shopId = $request->user->shop_id ?? 0;
        $res = $model->upload($request, 'file', false, $shopId, 'excel');
        if (empty($res['file_src']) || !file_exists('./' . $res['file_src'])) {
            return [
                'code' => 100,
                'msg' => !empty($res['msg']) ? $res['msg'] : '上传失败',
                'fileName' => $res['file_src'] ?? ''
            ];
        }
        if (!empty($request->get('updateSupport'))) {
            $request['type'] = $request->get('updateSupport');
        }
        $request['file_name'] = $res['file_name'];
        $request['upload_rout'] = $res['file_src'];

        return $this->repoist->add($request);
    }
}
